#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e5+10;

vector<int> stk[N];
int n;
ll s[N],c[N],f[N],col[N];

ll Y(int x){return f[x-1] + s[x] * c[x] * c[x] - 2 * c[x] * s[x];}

ll X(int x){return c[x];}

db slope(int x1,int x2){return 1.0 * (Y(x2) - Y(x1)) / (X(x2) - X(x1));}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in;
	for(int i = 1;i <= n;i++) s[i] = in,c[i] = ++col[s[i]];
	for(int i = 1;i <= n;i++){
		while(stk[s[i]].size() > 1 && slope(stk[s[i]][stk[s[i]].size()-2] , stk[s[i]].back()) < slope(stk[s[i]].back(),i)) stk[s[i]].pop_back();
		stk[s[i]].push_back(i);		
		while(stk[s[i]].size() > 1 && slope(stk[s[i]][stk[s[i]].size()-2] , stk[s[i]].back()) < 2.0 * s[i] * c[i]) stk[s[i]].pop_back();
		int j = stk[s[i]].back();
		f[i] = f[j-1] + 1ll * s[i] * (c[i] - c[j] + 1) * (c[i] - c[j] + 1);
	}
	printf("%lld\n",f[n]);
	return 0;
}

